package bean;

/**
 * Created by Administrator on 2018/6/25.
 */
import java.lang.String;
import java.lang.System;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class Bookinfo {
    private String id;
    private String name;
    private String author;
    private String press;
    private String date;

    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getAuthor() {
        return author;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getDate() {
        return date;
    }

    public void setPress(String press) {
        this.press = press;
    }

    public String getPress() {
        return press;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getId() {
        return id;
    }

    public static ArrayList<Bookinfo> getbooklist(){
        ArrayList<Bookinfo> list=new ArrayList<Bookinfo>();
        String sql="select * from sctu_book";
        bean.dbbean jdbc=new bean.dbbean();
        ResultSet rs=jdbc.executeQuery(sql);
        try{
            while(rs.next()){
                Bookinfo bi=new Bookinfo();
                bi.setId(rs.getString("id"));
                bi.setName(rs.getString("name"));
                bi.setAuthor(rs.getString("author"));
                bi.setPress(rs.getString("press"));
                bi.setDate(rs.getString("date"));
                list.add(bi);
            }
            rs.close();
        }
        catch(SQLException e){
            e.printStackTrace();
        }
        jdbc.close();
        return list;
    }

    public static Bookinfo getbookbyid(String id){
        String sql="select * from mvc_book where id="+id;
        dbbean jdbc=new dbbean();
        ResultSet rs=jdbc.executeQuery(sql);
        Bookinfo bi = new Bookinfo();
        try{
            if(rs.next()){
                bi.setDate(rs.getString("date"));
                bi.setPress(rs.getString("press"));
                bi.setAuthor(rs.getString("author"));
                bi.setName(rs.getString("name"));
                bi.setId(rs.getString("id"));
            }
            rs.close();
        }
        catch(SQLException e){
            System.out.println("no find");
        }
        jdbc.close();
        return bi;
    }


    public static int updatebook(String id,String name,String author,String press,String date){
        int result=0;
        String sql="update mvc_book set name='"+name+"',author='"+author+"',press='"+press+"',date='"+date+"' where id="+id;
        dbbean jdbc=new dbbean();
        result=jdbc.executeUpdate(sql);
        return result;
    }

    public static int deletebook(String id){
        int result=0;
        String sql="delete from mvc_book where id="+id;
        dbbean jdbc=new dbbean();
        result=jdbc.executeUpdate(sql);
        return result;
    }

    public static int addbook(String id,String name,String author,String press,String date){

        int result=0;
        String sql="insert into mvc_book(id,name,author,press,date) values(id,'"+name+"','"+author+"','"+press+"','"+date+"')";
        dbbean jdbc=new dbbean();
        result=jdbc.executeUpdate(sql);
        return result;
    }

}

